package com.pango.system.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.pango.system.model.User;
import com.pango.system.services.UserService;

/**
 * ClassName:LoginController <br/>
 * Date:     2014-5-26 下午4:44:35 <br/>
 * @author   thierry.fu
 * @version  
 * @since    JDK 1.6
 * @see 	 
 */
@Controller()
public class LoginController {
	
	@Autowired
	private UserService userService;
	
    @RequestMapping(value = {"/{login:login;?.*}"})
    public String login(HttpServletRequest req, Model model) {
    	
        Subject subject = SecurityUtils.getSubject();
        /**
    	 * 如果用户已经登录，先做一次退出操作
    	 */
        if (subject != null && subject.isAuthenticated()) {
            return "sys/main";
        }
        //end
        String exceptionMsg = (String)req.getAttribute(FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME);
        if(exceptionMsg != null) {
            model.addAttribute("msg",exceptionMsg);
            return "login";
        }
        //查询功能列表
        return "login";
    }
    
    @RequestMapping(value = "/register")
    public String register(User user) {
    	userService.createUser(user);
    	return "register";
    }
    
    @RequestMapping("/")
    public String index(Model model) {
    	return "sys/main";
    }
    
    
}
